package com.ruoyi.activiti.controller;

import com.ruoyi.activiti.condition.ProcessDefinitionCondition;
import com.ruoyi.activiti.condition.TodoCondition;
import com.ruoyi.activiti.service.impl.ActivitiServiceImpl;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import javafx.scene.control.Tab;
import org.apache.ibatis.annotations.Mapper;
import org.apache.xmlbeans.impl.common.IOUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * @author danghf
 * @date 2024/04/23 21:28:39
 */

@RestController
@RequestMapping("/bpmn")
public class ActivitiController extends BaseController {

    private final ActivitiServiceImpl activitiService;

    public ActivitiController(ActivitiServiceImpl activitiService) {
        this.activitiService = activitiService;
    }

    /**
     * 部署流程
     */
    @PostMapping("/deployment")
    public AjaxResult deployment(@RequestParam("file") MultipartFile file){
       return activitiService.deployment(file);
    }

    /**
     * 获取文件资源
     * @param deploymentKey 部署key
     * @param deploymentVersion 部署版本
     * @param resourceType 资源类型
     *
     */
    @GetMapping("/fileResource")
    public void getFileResource(String deploymentKey,String deploymentVersion, String resourceType, HttpServletResponse response) throws IOException {
        InputStream fileResource = activitiService.getFileResource(deploymentKey, deploymentVersion, resourceType);
        ServletOutputStream outputStream = response.getOutputStream();
        IOUtil.copyCompletely(fileResource, outputStream);
    }


    /**
     * 获取流程定义
     * @param condition 查询条件
     */
    @GetMapping("/processDefinition")
    public TableDataInfo getProcessDefinitionKeys(ProcessDefinitionCondition condition){
        startPage();
        return getDataTable(activitiService.getProcessDefinitionKeys(condition));
    }

    /**
     * 撤销流程部署
     * @param deploymentId 部署id
     * @param cascade 是否级联
     */
    @PostMapping("/cancelDeployment")
    public AjaxResult cancelDeployment(String deploymentId, boolean cascade){
        return activitiService.cancelDeployment(deploymentId, cascade);
    }

    /**
     * 我的待办任务
     */
    @GetMapping("/getMeToDone")
    public TableDataInfo getMeToDone(TodoCondition condition){
        startPage();
        return getDataTable(activitiService.getMeToDone(condition));
    }

    /**
     * 全部待办任务
     */
    @GetMapping("/getAllToDone")
    public TableDataInfo getAllToDone(TodoCondition condition){
        startPage();
        return getDataTable(activitiService.getAllToDone(condition));
    }

    /**
     * 我的已办任务(操作记录)
     */
    @GetMapping("/getCompletedTaskWithMe")
    public TableDataInfo getCompletedTaskWithMe(TodoCondition condition){
        startPage();
        return getDataTable(activitiService.getCompletedTaskWithMe(condition));
    }

    /**
     * 完成任务
     * @param taskId 任务id
     * @param variables 任务参数
     */
    @PostMapping("/completeTask")
    public AjaxResult completeTask(String taskId, @RequestBody Map<String,Object> variables){
        return activitiService.completeTask(taskId, variables);
    }

    /**
     * 强制终止流程实例
     */
    @PostMapping("/forceTerminateProcessInstance")
    public AjaxResult forceTerminateProcessInstance(String processInstanceId, String message){
        return activitiService.forceTerminateProcessInstance(processInstanceId, message);
    }

    /**
     * 获取所有流程实例，包括完成与未完成，未完成的展示当前到达哪个阶段
     */
    @GetMapping("/getProcessInstances")
    public TableDataInfo getProcessInstances(TodoCondition condition){
        startPage();
        return getDataTable(activitiService.getProcessInstances(condition));
    }

    /**
     * 我的发起
     */
    @GetMapping("/getProcessInstanceByUserId")
    public TableDataInfo getProcessInstanceByUserId(TodoCondition condition){
        startPage();
        return getDataTable(activitiService.getProcessInstanceByUserId(condition));
    }

    /**
     * 启动一个流程没有业务表单
     * @param processDefinitionKey 流程定义key
     * @param variables 参数
     */
    @PostMapping("/startByProcessDefinitionKey")
    public AjaxResult startByProcessDefinitionKey(String processDefinitionKey, @RequestBody Map<String, Object> variables){
        return activitiService.startByProcessDefinitionKey(processDefinitionKey, variables);
    }

    /**
     * 驳回任务
     * @param taskId 任务id
     */
    @PostMapping("/rejected")
    public AjaxResult rejected(String taskId){
        return activitiService.rejected(taskId);
    }

    /**
     * 获取流程图
     * @param processInstanceId 流程实例id
     */
    @GetMapping("/getProcessDiagram")
    public void getProcessDiagram(String processInstanceId, HttpServletResponse response){
        InputStream processDiagram = activitiService.getProcessDiagram(processInstanceId);
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            IOUtil.copyCompletely(processDiagram, outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
